//index.js
//获取应用实例
let app = getApp();
import refreshLoadManager from '../../../utils/refreshLoadRegister.js'
import { basicData } from '../../../js/netCase/caseList/caseListData.js'
import ecfsConfig from '../../../config/ecfs.config.js'
import locale from '../../../config/locale/zh-CN/netCase/caseList.js'
import { merge } from '../../../utils/util.js'
import * as caseListBusiness from '../../../js/netCase/caseList/caseListBusiness.js'
import * as caseListApi from '../../../api/caseList.js'
import * as userApi from '../../../api/user.js'
Page({
  data: {
    pageNum: 1,
    pageSize: 10,
    caseList: [],
    cardNum: '',
    navData: basicData.navData,
    caseStatusStyle: basicData.caseStatusStyle,
    statusCode: 0,//案件状态code
    lastRequestResultsIsLessThanPageSize: false, //上次列表请求结果是否小于pagSize
    locale,
  },
  onLoad: function () {
    let query = wx.createSelectorQuery();
    query.select('#head').boundingClientRect()
    query.exec(res => {
      console.log('---------------')
      let config = {
        upHeight: res[0].height
      }
      refreshLoadManager.register(this, config);
      //获取data  
      this.doLoadData(1, 10);
    })
  },
  onReady() {
    // let query = wx.createSelectorQuery();
    // query.select('#head').boundingClientRect()
    // query.exec(res => {
    //   console.log(res)
    // })
  },
  doLoadData(pageNum, pageSize) {
    var that = this
    wx.showLoading({
      title: 'loading...',
    })

    const userConfig = {
      url: (ecfsConfig.SERVER_DOMAIN + ecfsConfig.GET_USERINFO_URL)
    }
    let caseConfig = {
      url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.CASE_LIST_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.CASE_LIST_URL),
      data: {
        certificateNumber: this.data.cardNum,
        pagenum: pageNum,
        pagesize: pageSize,
        status: this.data.statusCode
      },
    }
    if (this.data.cardNum) {
      caseListApi.getCaseList(caseConfig).then(res => {
        wx.hideLoading()
        console.log(res.data)
        refreshLoadManager.loadFinish(this, true)
        caseListBusiness.handleCaseList(this, res.data)
      }).catch(error => {
        console.log(error)
        wx.hideLoading()
        this.data.pageNum = that.data.pageNum === 1 ? 1 : this.data.pageNum - 1
      })
    } else {
      userApi.getUserInfo(userConfig).then(res => {
        this.data.cardNum = res.data.body.cardNum
        let _caseConfig = merge({}, caseConfig)
        _caseConfig.data.certificateNumber = res.data.body.cardNum
        return caseListApi.getCaseList(_caseConfig)
      }).then(res_ => {
        wx.hideLoading()
        console.log(res_.data)
        refreshLoadManager.loadFinish(this, true)
        caseListBusiness.handleCaseList(this, res_.data)
      }).catch(error => {
        console.log(error)
        wx.hideLoading()
        this.data.pageNum = that.data.pageNum === 1 ? 1 : this.data.pageNum - 1
      })
    }
    // userApi.getUserInfo(config).then(res => {
    //   if (res.data.code === 200) {
    //     this.data.cardNum = res.data.body.cardNum
    //     const config = {
    //       url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.CASE_LIST_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.CASE_LIST_URL),
    //       data: {
    //         certificateNumber: res.data.body.cardNum,
    //         pagenum: pageNum,
    //         pagesize: pageSize,
    //         status: that.data.statusCode
    //       },
    //     }

    //     return caseListApi.getCaseList(config)
    //   }
    // }).then(res_ => {
    //   wx.hideLoading()
    //   console.log(res_.data)
    //   refreshLoadManager.loadFinish(that, true)
    //   caseListBusiness.handleCaseList(that, res_.data)
    // }).catch(error => {
    //   console.log(error)
    //   wx.hideLoading()
    //   that.data.pageNum = that.data.pageNum === 1 ? 1 : that.pageNum - 1
    // })
  },
  //下拉刷新数据
  refresh() {
    this.data.pageNum = 1
    this.doLoadData(this.data.pageNum, this.data.pageSize)
  },
  //加载更多数据
  loadMore() {
    this.doLoadData(this.data.pageNum, this.data.pageSize)
  },
  chooseCaseStatus(e) {
    const code = e.target.dataset.code

    if (this.data.statusCode !== code) {
      this.data.navData[this.data.statusCode].checked = false
      this.data.navData[code].checked = true
      this.data.statusCode = code
      this.setData({
        navData: this.data.navData
      })
      this.refresh()
    }
  },
  getCaseInfo(e) {
    let that = this
    const config = {
      url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.CASE_INFO_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.CASE_INFO_URL),
      data: {
        caseId: e.target.dataset.caseid
      },
    }
    caseListApi.getCaseInfo(config).then(res => {
      wx.navigateTo({
        url: '/pages/netCase/caseInfo/caseInfo?caseId=' + e.target.dataset.caseid,
      })
      caseListBusiness.handleCaseInfo(that, res.data)
    }).catch(error => {
      console.log(error)
    })
  },
  startRegisterCase() {
    const operateCaseStateList = {
      isAdd: true,
      isHasAccuser: false,
      isHasAppellee: false,
      isHasRequest: false,
      isHasFact: false,
    }
    wx.setStorage({
      key: 'operateCaseStateList',
      data: operateCaseStateList,
    })
    wx.setStorage({
      key: 'caseId',
      data: '',
    })
    wx.navigateTo({
      url: '/pages/netCase/registerCase/registerCase',
    })
  },
  skipToEvidence(e) {
    if (e.target.dataset.type) {
      wx.navigateTo({
        url: '/pages/netCase/evidenceList/evidenceList?pageIsFrom=2',
      })
    }else {
      wx.navigateTo({
        url: '/pages/netCase/evidenceList/evidenceList',
      })
    }
  },

  /** 
   * 旋转上拉加载图标 
   */
  // function updateRefreshIcon() {
  //   let deg = 0;
  //   let _this = this;
  //   console.log('旋转开始了.....')
  //   let animation = wx.createAnimation({
  //     duration: 1000
  //   });

  //   let timer = setInterval(function () {
  //     if (!_this.data.refreshing)
  //       clearInterval(timer);
  //     animation.rotateZ(deg).step();//在Z轴旋转一个deg角度  
  //     deg += 360;
  //     _this.setData({
  //       refreshAnimation: animation.export()
  //     })
  //   }, 1000);
  // }
})